﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lync.Widget.Library.Presentation.Entities;
using Lync.Widget.Library.Presentation.Models;
using Lync.Widget.Library.Presentation.Presenters;

namespace Lync.Widget.Library.Presentation.Views
{
    public class GroupsView : UserControl, IGroupsView
    {
        #region Controls

        protected TextBox OrganisationUnitTextBox;
        protected Repeater GroupsRepeater;
        protected Label ErrorMesssageLabel;
        protected PlaceHolder GroupsResultPlaceHolder;
        protected PlaceHolder SearchPlaceHolder;
        protected Button SaveButton;
        protected Label SavedLabel;
        protected PlaceHolder SavedPlaceHolder;
        protected Button DownloadButton;
        protected TextBox GlobalQuantityTextBox;
        protected TextBox GlobalTimeToWaitTextBox;

        #endregion

        #region View Properties

        public IGroupsPresenter Presenter { get; private set; }

        public string OrganisationUnit
        {
            get { return OrganisationUnitTextBox.Text; }
            set { OrganisationUnitTextBox.Text = value; }
        }

        public IList<IGroup> SelectedGroups
        {
            get
            {
                var temp = new List<IGroup>();
                FillSelectedGroups(temp);
                return temp;
            }
        }
        public bool ShowSaveButton
        {
            set { SaveButton.Visible = value; }
        }
        public bool ShowDownloadButton
        {
            set { DownloadButton.Visible = value; }
        }
        public bool ShowSearch
        {
            set { SearchPlaceHolder.Visible = value; }
        }
        public bool Saved
        {
            set
            {
                SavedPlaceHolder.Visible = value;                
            }
        }
        public int TimeToWait
        {
            get { return Convert.ToInt32(GlobalTimeToWaitTextBox.Text); }
            set { GlobalTimeToWaitTextBox.Text = value.ToString(); }
        }
        public int HuntgroupQuantity
        {
            get { return Convert.ToInt32(GlobalQuantityTextBox.Text); }
            set { GlobalQuantityTextBox.Text = value.ToString(); }
        }
        public IList<IGroup> Groups
        {
            set
            {
                GroupsRepeater.DataSource = value;
                GroupsRepeater.DataBind();
            }
        }
        public string ErrorMessage
        {
            get { return ErrorMesssageLabel.Text; }
            set
            {
                ErrorMesssageLabel.Text = value;
                ErrorMesssageLabel.Visible = !String.IsNullOrEmpty(value);
            }
        }

        public bool ShowResults
        {
            set { GroupsResultPlaceHolder.Visible = value; }
        }
        public IGroupMembersView MembersView(int index)
        {
            return GroupsRepeater.Items[index].FindControl("MembersOf") as GroupMembersView;
        }

        #endregion


        #region Control Events

        protected override void OnInit(EventArgs e)
        {
            Presenter = new GroupsPresenter(this, new GroupsModel());
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
            {
                Presenter.OnLoad();
            }
        }

        #endregion

        //protected void EditMembersButton_OnClick(object sender, EventArgs e)
        //{
        //    var button = (Button)sender;
        //    var itemIndex = Convert.ToInt32(button.CommandArgument);
        //    var i = 0;

        //    for (int j = 0; j < GroupsRepeater.Items.Count; j++)
        //    {
        //        var view = MembersView(i);

        //        if (view != null)
        //        {
        //            view.Visible = i == itemIndex;
        //        }

        //        i++;
        //    }
        //}

        protected void SearchGroupsButton_OnClick(object sender, EventArgs e)
        {
            Presenter.Search();
        }

        protected void SaveButton_OnClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Presenter.OnSave();
            }
        }


        private void FillSelectedGroups(List<IGroup> groups)
        {
            var items = (from item in GroupsRepeater.Items.Cast<RepeaterItem>().ToList()
                         where (((CheckBox)item.FindControl("GroupCheckBox")).Checked)
                         select new Group
                         {
                             DistinguishedName = ((HiddenField)item.FindControl("GroupDistinguishedName")).Value,
                             Id = ((TextBox)item.FindControl("GroupIdTextBox")).Text,
                             Name = ((Label)item.FindControl("GroupLabel")).Text,
                             HuntGroupSettings = new HuntGroupSetting
                             {
                                 TimeToWait = Convert.ToInt32(((TextBox)item.FindControl("TimeToWaitTextBox")).Text),
                                 UserCount = Convert.ToInt32(((TextBox)item.FindControl("QuantityTextBox")).Text),
                             },
                             Members = (((IGroupMembersView)item.FindControl("MembersOf"))).SelectedMembers,
                         });


            groups.AddRange(items);
        }

        protected void DownloadButton_OnClick(object sender, EventArgs e)
        {
            Presenter.OnDownload(HttpContext.Current);
        }

        protected void SelectAllCheckBox_OnChanged(object sender, EventArgs e)
        {
            var checkbox = (CheckBox)sender;
            GroupsRepeater.Items.Cast<RepeaterItem>().ToList().ForEach(item => ((CheckBox)item.FindControl("GroupCheckBox")).Checked = checkbox.Checked);
        }
    }
}